﻿using System;

namespace PropertyExpression.ReactiveRelation
{
    public static partial class Value
    {
        public static IPropertyHolder<TTarget> Convert<TSource, TTarget>(
            this IPropertyHolder<TSource> propertyHolder, Func<TSource, TTarget> func)
        {
            return new PropertyHolderConverter<TSource, TTarget>(propertyHolder, func);
        }

        private class PropertyHolderConverter<TSource, TTarget> : IPropertyHolder<TTarget>
        {
            private readonly IPropertyHolder<TSource> propertyHolder;
            private readonly Func<TSource, TTarget> func;

            public PropertyHolderConverter(IPropertyHolder<TSource> propertyHolder, Func<TSource, TTarget> func)
            {
                this.propertyHolder = propertyHolder;
                this.func = func;
            }

            public IPropertyValue<TProperty> GetPropertyValue<TProperty>(
                IPropertyExpressionProxy<TTarget, TProperty> propertyExpressionProxy)
            {
                return propertyHolder.GetPropertyValue(
                    new ExpressionWrapperConverter<TSource, TTarget, TProperty>(
                        propertyExpressionProxy, func
                        )
                    );
            }
        }
    }
}
